package com.ext.aic.utils;

import android.app.Activity;
import android.os.Bundle;

import com.onegravity.rteditor.RTEditText;
import com.onegravity.rteditor.RTManager;
import com.onegravity.rteditor.api.RTApi;
import com.onegravity.rteditor.api.RTMediaFactory;
import com.onegravity.rteditor.api.RTMediaFactoryImpl;
import com.onegravity.rteditor.api.RTProxyImpl;
import com.onegravity.rteditor.api.media.RTAudio;
import com.onegravity.rteditor.api.media.RTImage;
import com.onegravity.rteditor.api.media.RTVideo;

public class RTEditUtils {
    private static volatile RTEditUtils instance;

    public static RTEditUtils getInstance() {
        if (instance == null) {
            synchronized (RTEditUtils.class) {
                if (instance == null) {
                    instance = new RTEditUtils();
                }
            }
        }
        return instance;
    }

    private RTManager rtManager;
    private RTApi rtApi;

    public void initForActivity(Activity activity, Bundle savedInstanceState) {
        if (rtApi == null) {
            RTMediaFactoryImpl rtMediaFactory = new RTMediaFactoryImpl(activity, true);
            rtApi = new RTApi(
                    activity,
                    new RTProxyImpl(activity),
                    rtMediaFactory
            );
        }
        if (rtManager == null) {
            rtManager = new RTManager(rtApi, savedInstanceState);
        }
    }

    public void registerEditor(Activity activity, RTEditText editRte) {
        if (rtApi == null || rtManager == null) {
            return;
        }
        RTMediaFactory<RTImage, RTAudio, RTVideo> mediaFactory = rtApi.getMediaFactory();
        rtManager.registerEditor(editRte, true);
    }

    public void onSaveInstanceState(Bundle outState) {
        if (rtManager == null) {
            return;
        }
        rtManager.onSaveInstanceState(outState);
    }

    public void destroy() {
        if (rtManager == null) {
            return;
        }
        rtManager.onDestroy(true);
        rtManager = null;
        rtApi = null;
    }

    public void undo() {
        if (rtManager == null) {
            return;
        }
        rtManager.onUndo();
    }
}
